// pages/orderInfo/orderInfo.js
let app = getApp();
let API = require('../../api.js').API;
let util = require('../../utils/util');
let req = require('../../utils/request').Request;
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },
  /**
   * 进入聊天界面
   */
  enterChat: function (event) {
    //console.log(event);
    let id = event.currentTarget.dataset.target.id;
    wx.navigateTo({
      url: '/pages/myChat/chatWindow/chatWindow',
      success: (res) => {
        console.log(res);
        res.eventChannel.emit('sendDestinationId', {
          destinationUser: {
            id:id,
            name:event.currentTarget.dataset.target.name,
            online:online
          }
        });
      }
    });

    let chatPeople = app.globalData.chatPeople;
    let online=false;
    chatPeople.forEach(function(item){
      if(item.id==id){
        online=item.online
      }
    })

    if(!chatPeople.some(function(value){
      return value.id==id;
    })){
      //待补充对方是否在线的判断
      //。。。。
      chatPeople.push({
        id:id,
        offlineMessage:false,
        online:true
      });
    }
    
    console.log(chatPeople);
  },


  /**
   * 获取订单详细信息
   */
  getOrderInfo() {
    let that = this;
    req.get(API.getOrderInfo + '?token=' + app.globalData.token + '&orderId=' + this.data.orderId)
      .then(function (res) {
        //console.log(res);
        if (res.code == 0) {
          that.setData({
            order: res.dataMap.order
          });
        }
      })
      .catch(function (res) {
        console.log(res);
      })
  },
  receiveOrder: function () {
    req.get(API.recieveOrder + '?token=' + app.globalData.token + '&orderId=' + this.data.orderId)
      .then(function (res) {
        console.log(res);
        util.msg(res.msg);
      }).catch(function (res) {
        console.log(res);
      })
  },
  finishOrder: function () {
    req.get(API.finishOrder + '?token=' + app.globalData.token + '&orderId=' + this.data.orderId)
      .then(function (res) {
        console.log(res);
        util.msg(res.msg);
      }).catch(function (res) {
        console.log(res);
      })
  },
  checkOrder: function () {
    req.get(API.checkOrder + '?token=' + app.globalData.token + '&orderId=' + this.data.orderId)
      .then(function (res) {
        console.log(res);
        util.msg(res.msg);
      }).catch(function (res) {
        console.log(res);
      })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      orderId: options.id
    });
    this.getOrderInfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})